#!/usr/bin/perl

use Modern::Perl;
use diagnostics;
use List::Util qw(shuffle);

my @genomaA_genes;
my @genomaA_breaks;
my @genomaB_genes;
my @genomaB_breaks;

print "Quantidade de genes: ";
my $qnt_genes = <>;
$qnt_genes = int($qnt_genes);

my $max_breaks = $qnt_genes - 1;

@genomaA_genes = shuffle (1..$qnt_genes);
@genomaB_genes = shuffle (1..$qnt_genes);


print "Quantidade de breaks no genoma A (-1 para aleatório, max = " . $max_breaks . "): ";
my $qnt_breaksA = <>;
chomp $qnt_breaksA;

$qnt_breaksA = int(rand($qnt_genes)) if($qnt_breaksA < 0 or $qnt_breaksA > $max_breaks);

print "Quantidade de breaks no genoma B (-1 para aleatório, max = " . $max_breaks  . "): ";
my $qnt_breaksB = <>;
chomp $qnt_breaksB;

$qnt_breaksB = int(rand($qnt_genes)) if($qnt_breaksB < 0 or $qnt_breaksB > $max_breaks);

foreach (1..($qnt_genes-1))
{
	push @genomaA_breaks, 'n';
	push @genomaB_breaks, 'n';
}

foreach (1..($qnt_breaksA))
{
	$genomaA_breaks[$_ - 1] = 'b';
}

@genomaA_breaks = shuffle (@genomaA_breaks);

foreach (1..($qnt_breaksB))
{
	$genomaB_breaks[$_ - 1] = 'b';
}

@genomaB_breaks = shuffle (@genomaB_breaks);

open my $FH, '>', 'randinput.txt';

say $FH "$qnt_genes";
orientacao();
print $FH shift @genomaA_genes;
foreach my $i (0..(@genomaA_genes-1))
{
	print $FH " |" if $genomaA_breaks[$i] eq 'b';
	print $FH " ";
	orientacao();
	print $FH $genomaA_genes[$i];
}
print $FH "\n";


orientacao();
print $FH shift @genomaB_genes;
foreach my $i (0..(@genomaB_genes-1))
{
	print $FH " |" if $genomaB_breaks[$i] eq 'b';
	print $FH " ";
	orientacao();
	print $FH $genomaB_genes[$i];
}
print $FH "\n\n";

close $FH;

sub orientacao
{
	my $or = int(rand(2));

	if($or == 0)
	{
		print $FH '+';
	}
	else
	{
		print $FH '-';
	}
}

